package com.demo.camunda.util.ExecptionExecution;

import com.demo.camunda.util.CommonResult;
import com.demo.camunda.util.StaticValue;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class ExceptionTranslator {
    private final Logger log = LoggerFactory.getLogger(ExceptionTranslator.class);

    // 全局异常
    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public CommonResult errorHandler(Exception ex) {
        log.error(ex.getLocalizedMessage(), ex);
        return CommonResult.error(StaticValue.ERROR,ex.getMessage());
    }

    // 业务异常
    @ResponseBody
    @ExceptionHandler(value = ServiceException.class)
    public CommonResult handleBusinessException(ServiceException ex) {
        log.error(ex.getLocalizedMessage(), ex);
        return CommonResult.error(StaticValue.ERROR_SERVICE,ex.getMessage());
    }

}
